package com.gz.purchase.library.dao;

import android.content.Context;


import com.gz.purchase.library.pojo.UserInfo;

import net.tsz.afinal.FinalDb;

import java.util.List;

/**
 * 用户相关的增删改查
 * author wanner1991
 */
public class UserInfoDao {
    private FinalDb mFinalDb;

    public UserInfoDao(Context context) {
        this.mFinalDb = FinalDb.create(context);
    }

    /**
     * 保存用户信息
     * @param model 账户实体
     */
    public void insert(UserInfo model){
        synchronized (UserInfoDao.class){
            mFinalDb.save(model);
        }
    }

    /**
     * 删除所有用户
     */
    public void delete(){
        synchronized (UserInfoDao.class){
            mFinalDb.deleteByWhere(UserInfo.class, null);
        }
    }

    /**
     * 根据id删除用户
     * @param uid
     */
    public void deleteById(int uid){
        synchronized (UserInfoDao.class){
            mFinalDb.deleteByWhere(UserInfo.class,"user_id="+uid);
        }
    }

    /**
     * 获取用户，本地数据库用户表只会存储一个用户信息
     * @return
     */
    public UserInfo query(){
        synchronized (UserInfoDao.class){
            List<UserInfo> models = mFinalDb.findAll(UserInfo.class);
            if (models != null && models.size() > 0){
                return models.get(0);
            }
            return null;
        }
    }
    public UserInfo queryByUid(int userId){
        synchronized (UserInfoDao.class){
            UserInfo userInfo = mFinalDb.getUnique(UserInfo.class,"user_id="+userId);
            return userInfo;
        }

    }


}
